﻿using System;
using System.Globalization;

namespace ComplexNumberLibrary
{
    public class ComplexNumber
    {
        public ComplexNumber()
        {
            Real = 0;
            Image = 0;
        }

        public ComplexNumber(double real, double image)
        {
            Real = real;
            Image = image;
        }

        public ComplexNumber(ComplexNumber number)
        {
            Real = number.Real;
            Image = number.Image;
        }

        public double Real { get; set; }

        public double Image { get; set; }

        public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
        {
            double realVal = c1.Real + c2.Real;
            double imageVal = c1.Image + c2.Image;
            return new ComplexNumber(realVal, imageVal);
        }

        public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2)
        {
            double realVal = c1.Real - c2.Real;
            double imageVal = c1.Image - c2.Image;
            return new ComplexNumber(realVal, imageVal);
        }

        public static ComplexNumber operator *(ComplexNumber c1, ComplexNumber c2)
        {
            double realVal = c1.Real * c2.Real - c1.Image * c2.Image;
            double imageVal = c1.Image * c2.Real + c1.Real * c2.Image;
            return new ComplexNumber(realVal, imageVal);
        }

        public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
        {
            double realVal = (c1.Real * c2.Real + c1.Image * c2.Image) /
                             (c2.Real * c2.Real + c2.Image * c2.Image);
            double imageVal = (c1.Image * c2.Real - c1.Real * c2.Image) /
                              (c2.Real * c2.Real + c2.Image * c2.Image);
            return new ComplexNumber(realVal, imageVal);
        }

        public double Modulus()
        {
            double z = Math.Sqrt(Real*Real + Image*Image);
            return z;
        }

        public override bool Equals(Object obj)
        {
            if (obj == null)
            {
                return false;
            }

            var c = obj as ComplexNumber;
            if ((Object)c == null)
            {
                return false;
            }

            return (Real == c.Real) && (Image == c.Image);
        }

        public bool Equals(ComplexNumber c)
        {
            if ((Object)c == null)
            {
                return false;
            }

            return (Real == c.Real) && (Image == c.Image);
        }

        public override int GetHashCode()
        {
            return ((int)Real) ^ ((int)Image);
        }

        public override string ToString()
        {
            String num = Real.ToString("#");
            if (Image > 0 && Real != 0)
            {
                num += "+" + Image.ToString("#") + "i";
            } else if (Image != 0)
            {
                num += Image.ToString("#") + "i";
            }
            return num;
        }
    }
}